<?php

/**
 * Created by PhpStorm.
 * User: Yieong
 * Date: 16/4/5
 * Time: 下午4:49
 */
class GoodsController extends AdminBaseController {
	
	public function index(){
		$this->setTitle('物品列表', '数据需从Excel导入');
	
		$filter = new PubFilter();
		$grid   = new PubGrid();

        $filter->addFilter('g_id', 'input', '物品标识')
               ->addFilter('name', 'input', '物品名称');
		//$filter->addExport();
		$url = PubFunc::buildUrl("/oss/?r=goods/importGoods");
		$filter->addImport('物品导入',$url);
		
		$params = $filter->get();
		$header = GoodsService::indexHeader();
		if(PubFunc::isAjax()){
			$result = GoodsService::indexData($params);
			
			$this->out = array(
					'count'     => $result['count'],
					'data'      => $result['data'],
					'result'    => true
			);
			$this->outType = 'json';
		}else{
			$order  = 'id';
			$params['m'] = PubRequest::R('m');
			$url = PubFunc::ajaxUrl("?r=goods/index", $params);
			$grid->addGrid($header, $url, array(
					'name'=>'数据',
					'order'=>array($order, 'asc'),
					'type'=>'goods/index',
			));
			
			$this->out['grid'] = $grid->show();
			$this->out['filter'] = $filter->show();
			
			$this->out['downUrl'] = '/public/template/item.xlsx';
			$user = AccAuth::me();
			if($user['IsAdmin'] == 1 || $user['Identity'] == 2){
				$this->out['table'] = 'goods';
			}
			
		}
	}
	
	public function delete(){
		$id = $this->R('id');
	
		$result = GoodsService::delete($id);
	
		if($result !== false){
			$this->showMessage('删除成功', PubFunc::buildUrl("/oss/?r=goods/index"));
		}else{
			$this->showMessage('删除失败');
		}
	}
	
	public function importGoods(){
		ini_set('post_max_size', '5M');
		ini_set('upload_max_filesize','5M');
		$upload = GoodsService::upload($_FILES);
		if($upload["status"] == 1){ //上传成功
			$filePath = $upload['data'];
			$data = LibExcel::import($filePath);
			$result = GoodsService::xlsAdd($data);
			$out = $result;
		}else{
			$out = $upload; //上传失败
		}
		
		$this->out = $out;
		$this->outType = 'json';
	}

    public function autoComplete(){
    	$val = $this->R('val');
    	$type = $this->R('type');
    	
    	$result = GoodsService::autoComplete($val,$type);
    	
    	$out = array('status'=>1,'msg'=>'查询成功','data'=>$result);
    	$this->out = $out;
    	$this->outType = 'json';
    }
    
    public function getValue(){
    	$val = $this->R('val');
    	$type = $this->R('type');
    	if($type == 1){
    		$result = GoodsService::getGid($val,$type);
    	}else if($type == 2){
    		$result = GoodsService::getName($val,$type);
    	}

    	$out = array('status'=>1,'msg'=>'查询成功','data'=>$result);
    	$this->out = $out;
    	$this->outType = 'json';
    }

    

    
}